Relative Strength Index (RSI)¶

In [1]:
import yfinance as yf
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.subplots as sp
import pandas as pd
In [2]:
ticker = "V"
data = yf.download(ticker, interval="1d")
[*********************100%***********************]  1 of 1 completed
In [3]:
# code from an article
change = data["Close"].diff()
change.dropna(inplace=True)
change_up = change.copy()
change_down = change.copy()
change_up[change_up<0] = 0
change_down[change_down>0] = 0
change.equals(change_up+change_down)
avg_up = change_up.rolling(14).mean()
avg_down = change_down.rolling(14).mean().abs()
data["RSI"] = 100 * avg_up / (avg_up + avg_down)
In [4]:
fig = sp.make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.01, row_heights=[0.8, 0.2])

# Add the candlestick chart to the first subplot
fig.add_trace(go.Candlestick(x=data.index,
                             open=data['Open'],
                             high=data['High'],
                             low=data['Low'],
                             close=data['Close']),
              row=1, col=1)
fig.add_trace(go.Scatter(x=data.index,
                         y=data['RSI'],  
                         mode='lines',
                         name='RSI'),
              row=2, col=1)
fig.update_layout(height=700, width=1000, title_text="Visa")

fig.update_layout(xaxis_rangeslider_visible=False)

fig.show()
In [5]:
money = 100

data["enter_rule"] = data["RSI"] < 20
data["exit_rule"] = data["RSI"] > 70
data["stocks"] = (money/ data.Close.shift(1)).apply(lambda x:round(x,0))
data["position"] = np.where((data.enter_rule),1,0)
data["exit"] = data.Open.shift(-1)
data["entry"] = data.Open
data["trade"] = (data.exit - data.entry) * data.stocks
data["gain"] = data.position * data.trade
data["equity"] = data.gain.cumsum() + money
In [6]:
data.drop(["enter_rule", "exit_rule", "exit", "entry" ], axis=1, inplace=True)
data.head(20)
Out[6]:
Open High Low Close Adj Close Volume RSI stocks position trade gain equity
Date
2008-03-19 14.875000 17.250000 13.750000 14.125000 12.702869 708486000 NaN NaN 0 NaN NaN NaN
2008-03-20 14.600000 16.250000 14.375000 16.087500 14.467784 198985200 NaN 7.0 0 15.469994 0.0 100.0
2008-03-24 16.809999 16.924999 14.750000 14.932500 13.429070 149566400 NaN 6.0 0 -9.989994 -0.0 100.0
2008-03-25 15.145000 16.062500 14.955000 15.812500 14.220469 87092000 NaN 7.0 0 3.762496 0.0 100.0
2008-03-26 15.682500 16.120001 15.392500 15.990000 14.380107 43111600 NaN 6.0 0 1.980005 0.0 100.0
2008-03-27 16.012501 16.184999 15.662500 15.727500 14.144030 39829600 NaN 6.0 0 -1.020006 -0.0 100.0
2008-03-28 15.842500 15.860000 15.540000 15.690000 14.110305 24675200 NaN 6.0 0 -1.155001 -0.0 100.0
2008-03-31 15.650000 15.870000 15.512500 15.590000 14.020372 22157200 NaN 6.0 0 0.690004 0.0 100.0
2008-04-01 15.765000 15.920000 15.077500 15.402500 13.851748 60410800 NaN 6.0 0 -2.010000 -0.0 100.0
2008-04-02 15.430000 15.580000 15.125000 15.580000 14.011383 43836400 NaN 6.0 0 0.344999 0.0 100.0
2008-04-03 15.487500 16.572500 15.455000 16.375000 14.726341 100868000 NaN 6.0 0 4.425001 0.0 100.0
2008-04-04 16.225000 16.330000 15.937500 16.120001 14.497017 36394400 NaN 6.0 0 0.299995 0.0 100.0
2008-04-07 16.275000 16.450001 16.004999 16.260000 14.622917 28301600 NaN 6.0 0 -0.299995 -0.0 100.0
2008-04-08 16.225000 17.059999 16.202499 17.022499 15.308647 53925600 NaN 6.0 0 5.310001 0.0 100.0
2008-04-09 17.110001 17.180000 16.402500 16.447500 14.791542 48324400 65.550732 6.0 0 -3.419998 -0.0 100.0
2008-04-10 16.540001 16.682501 16.277500 16.527500 14.863490 22372000 53.939131 6.0 0 -1.365005 -0.0 100.0
2008-04-11 16.312500 16.692499 16.312500 16.527500 14.863490 26421600 68.002278 6.0 0 2.174995 0.0 100.0
2008-04-14 16.674999 16.809999 16.382500 16.420000 14.766812 28443600 58.304864 6.0 0 -0.614994 -0.0 100.0
2008-04-15 16.572500 16.637501 16.000000 16.087500 14.467784 23888000 51.278688 6.0 0 -2.595005 -0.0 100.0
2008-04-16 16.139999 16.737499 16.139999 16.535000 14.870233 25136800 60.100070 6.0 0 2.550007 0.0 100.0
In [7]:
fig = px.line(data.equity)
fig.update_layout(title = "Equity",xaxis_title = "Time",height=600,width=1000)
fig.show()